//
//  LoginViewController.m
//  TheRoadAD
//
//  Created by baixue on 16/8/5.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPasswordView.h"
#import "PersonModel.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UILabel *logoIcon;//登录
@property (weak, nonatomic) IBOutlet UILabel *logintitle;
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;//用户名
@property (weak, nonatomic) IBOutlet UITextField *password;//密码
@property (nonatomic, strong) NSMutableArray *userArray;//

@property (nonatomic, strong) UIAlertView *alert;

@end

@implementation LoginViewController

-(NSMutableArray *)userArray{
    if (!_userArray) {
        _userArray = [NSMutableArray array];
    }
    return _userArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}
//返回
- (IBAction)backItem:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//登录按钮
- (IBAction)loginBtn:(id)sender {

    
    if ([_phoneNum.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入手机号码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([_password.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        
        [self getLogin];
    }
    
}
//注册按钮
- (IBAction)register:(id)sender {
    RegisterViewController *regiView = [[RegisterViewController alloc] init];
    [self presentViewController:regiView animated:YES completion:nil];
}
//忘记密码
- (IBAction)forgetPwd:(id)sender {
    ForgetPasswordView *forget = [[ForgetPasswordView alloc] init];
    [self presentViewController:forget animated:YES completion:nil];
}

//登录
-(void)getLogin{
// URL:http://114.215.172.41:7756/UserInfo/UserLogin
    
    NSString *key = [SHA1Secret getKeyWithActionName:@"UserLogin"];
    NSDictionary *para = @{
    
                           @"UserName":_phoneNum.text,
                           @"PassWord":_password.text,
                           @"key":key
                           };
    [NetWorkManager requestWithType:0 withUrlString:@"UserInfo/UserLogin"  withParaments:para withMethend:@"UserLogin" withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        
        if ([object[@"code"] isEqualToString:@"100"]) {
            //字符串转换为json数据
            NSArray *json = [NetWorkManager arrayWithJsonString:object[@"data"]];
            NSDictionary *dic = [json firstObject];
            PersonModel *model = [PersonModel mj_objectWithKeyValues:dic];
            [self.userArray addObject:model];
            [[NSUserDefaults  standardUserDefaults] setValue:dic[@"Id"] forKey:@"Id"];//id
            [[NSUserDefaults standardUserDefaults] setObject:_password.text forKey:@"password"];
           
            _alert = [[UIAlertView alloc] initWithTitle:@"提示" message:nil delegate:self cancelButtonTitle:object[@"message"] otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
            [_alert show];
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
    
}

//空白收键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void) performDismiss:(NSTimer *)timer
{
    [_alert dismissWithClickedButtonIndex:0 animated:NO];

}



@end